//============================================================================
// Name        : curlPost.cpp
// Author      : alex2stf
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <ctime>
#include <curl/curl.h>
#include <pthread.h>
#include <sstream>


#define NUM_THREADS     240000

using namespace std;


const std::string currentDateTime() {
    time_t     now = time(0);
    struct tm  tstruct;
    char       buf[80];
    tstruct = *localtime(&now);
    // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
    // for more information about date/time format
    strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);

    return buf;
}



void * sendData(void *threadid){
	long tid;
	   tid = (long)threadid;
	   printf("Hello World! It's me, thread #%ld!\n", tid);

	   string mystring;
	  	       stringstream mystream;
	  	       mystream << tid;
	  	       mystring = mystream.str();
	  	       cout << mystring << "\n";

	CURL *curl;
		CURLcode res;

		curl_global_init(CURL_GLOBAL_ALL);

		curl = curl_easy_init();
		std::string data="insert=thread"+currentDateTime();
		data.append("threadNr");
		data.append(mystring);

		if(curl){
				time_t t = time(0);
				curl_easy_setopt(curl, CURLOPT_URL, "http://errorlogger.cam4.com/stressTest.php");
				curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());

				res = curl_easy_perform(curl);

				 if(res != CURLE_OK){
					   fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
				 }
			     /* always cleanup */
			     curl_easy_cleanup(curl);
		}else{
					cout << "Fail to create curl handle for post method\n";
					exit(1);
		}

		 curl_global_cleanup();
		 pthread_exit(NULL);
}


int main(int argc, char* argv[]) {

//	cout << argv[1] << endl;

//	int num=2;
//	//long NUM_THREADS=2;
//	if(argv[1]){
//		num=atoi(argv[1]);
//	}
//
//	return 4;
	//sendData();


	pthread_t threads[NUM_THREADS];
	int rc;
	   long t;
	   pthread_attr_t        pta;
	   pthread_mutex_t    mutex = PTHREAD_MUTEX_INITIALIZER;
	   pthread_attr_init(&pta);
	   pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE);




	   for(t=0; t<NUM_THREADS; ++t){
		   printf("In main: creating thread %ld\n", t);
		   pthread_create(&threads[t], NULL, sendData, (void *)t);
		   if (rc){
		            printf("ERROR; return code from pthread_create() is %d\n", rc);
		            //exit(-1);

		         }
	   }


	   for (t=0; t<NUM_THREADS; ++t) {
	       rc = pthread_join(threads[t], NULL);
	       //checkResults("pthread_join()\n", rc);
	     }

	   pthread_attr_destroy(&pta);
	     pthread_mutex_destroy(&mutex);

	   pthread_exit(NULL);



	   string str;
	   cout << "Please enter an integer value: ";
	   while(str!="exit"){
		   cin >> str;
		   cout << "you typed " << str << endl;
		   if(str=="exit"){
			   exit(4);
		   }
	   }



	   cout << "FINALIZED" << endl;
//	pthread_t t1;
//	pthread_create(&t1, NULL, &sendData, NULL);
//	//void* result;
//	pthread_join(t1, NULL);


	return 0;
}
